﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VideoCarousel.Entity
{
    public class SqlSugarHelp
    {
        private static  string connStr;

        public static SqlSugar.SqlSugarClient GetInstance()
        {
            if (connStr == null)
            {
                connStr = $"Data Source={System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "db.db")}; Version=3;";
            }
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connStr,
                DbType =  DbType.Sqlite,
                IsAutoCloseConnection = true,
                InitKeyType= InitKeyType.Attribute
            });
            // db.Open();
            //每次Sql执行前事件
            //db.Aop.OnLogExecuting = (sql, pars) =>
            //{
            //    //我可以在这里面写逻辑
            //    Console.WriteLine("---------SQL----------");
            //    Console.WriteLine(sql);
            //    Console.WriteLine("---------END-----------");
            //};
            var client = db;
            return client;
        }
        public static  void CreateTable(params Type[] types)
        {
            var client = SqlSugarHelp.GetInstance();
            foreach (var item in types)
            {
                client.CodeFirst.InitTables(item);

            }
        }
        //public static int Install<T>(T t) where T : class,new()
        //{
        //    var db = SqlSugarHelp.GetInstance();
        //    //返回插入行数
        //   return  db.Insertable<T>(t).ExecuteCommand(); //都是参数化实现
        //}

        public static int Update(object updateObj)
        {
            var db = SqlSugarHelp.GetInstance();

            return db.Updateable(updateObj).ExecuteCommand();
        }

        public static List<T> GetAll<T>() where T : class
        {
            var db = SqlSugarHelp.GetInstance();
            return db.Queryable<T>().ToList();
        }
    }
}
